<template>
    <div>
        <el-row :gutter="20">
            <el-col :span="12">
                <el-card shadow="hover">
                    <template #header>
                        <div class="clearfix">
                            <span>基础信息</span>
                        </div>
                    </template>
                    <div class="info">
                        <div class="info-image" @click="showDialog">
                            <el-avatar :size="100" :src="user_msg.user_img" />
                            <span class="info-edit">
                                <i class="el-icon-lx-camerafill"></i>
                            </span>
                        </div>
                        <div class="info-name">{{ user_msg.user_nick_name }}</div>
                        <div class="info-desc">{{ user_msg.user_signature == null ? "不可能！我的代码怎么可能会有bug！" :
                            user_msg.user_signature }}</div>
                    </div>
                </el-card>
            </el-col>
            <el-col :span="12">
                <el-card shadow="hover">
                    <template #header>
                        <div class="clearfix">
                            <span>账户编辑</span>
                        </div>
                    </template>
                    <el-form label-width="90px">
                        <el-form-item label="用户账号:"> {{ user_msg.user_acc }} </el-form-item>
                        <el-form-item label="旧密码：">
                            <el-input type="password" v-model="form.old"></el-input>
                        </el-form-item>
                        <el-form-item label="新密码：">
                            <el-input type="password" v-model="form.new"></el-input>
                        </el-form-item>
                        <el-form-item label="个人简介：">
                            <el-input v-model="form.desc"></el-input>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="onSubmit">保存</el-button>
                        </el-form-item>
                    </el-form>
                </el-card>
            </el-col>
        </el-row>
        <el-dialog title="裁剪图片" v-model="dialogVisible" width="600px">
            <vue-cropper ref="cropper" :src="user_msg.user_img" :ready="cropImage" :zoom="cropImage"
                :cropmove="cropImage" style="width: 100%; height: 400px"></vue-cropper>

            <template #footer>
                <span class="dialog-footer">
                    <el-button class="crop-demo-btn" type="primary">选择图片
                        <input class="crop-input" type="file" name="image" accept="image/*" @change="setImage" />
                    </el-button>
                    <el-button type="primary" @click="saveAvatar">上传并保存</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script setup lang="ts" name="user">
import { reactive, ref, computed } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";

import VueCropper from "vue-cropperjs";
import "cropperjs/dist/cropper.css";
import avatar from "../assets/img/img.jpg";
import { useLoginStore } from "../store/user";
import { selectLoginUser, undateUserMsg, userUpdata, save_img, delete_img } from "../api/index";
let newImgUrl = ref("");
let oldImgUrl = ref("");

let loginUser = useLoginStore();
console.log(loginUser)
const name = localStorage.getItem("ms_username");
const form = reactive({
    old: "",
    new: "",
    desc: "不可能！我的代码怎么可能会有bug！",
});
// user_acc, user_pwd, new_pwd, new_signature
const onSubmit = () => {
    console.log(user_msg.value.user_acc, form.old, form.new, form.desc);
    undateUserMsg({ user_acc: user_msg.value.user_acc, user_pwd: form.old, new_pwd: form.new, new_signature: form.desc }).then((data) => {
        if (data.code == 200) {
            form.old = "";
            form.new = "";
            // console.log(data);
            slu();
        } else {
            ElMessage({
                message: data.msg,
                type: "error",
            });
        }
    });
};
let user_msg: any = computed(() => {
    return loginUser.$state.userLogin;
});
console.log('user_msg', user_msg.value)
oldImgUrl.value = user_msg.value.user_img;

const slu = () => {
    selectLoginUser({ admin_acc: user_acc }).then((data) => {
        // console.log(data.row[0]);
        loginUser.setLogin(data.row[0]);
        // console.log(user_msg.value);
        form.desc = user_msg.value.user_signature;
        oldImgUrl.value = user_msg.value.user_img;
    });
};
form.desc = user_msg.value.user_signature;
let arr = Object.keys(user_msg.value);
let user_acc = localStorage.getItem("ms_username");
if (arr.length == 0) {
    // console.log(user_msg.value, "user_msg.value");
    slu();
}
const avatarImg = ref(avatar);
const imgSrc = ref("");
const cropImg = ref("");
const dialogVisible = ref(false);
const cropper: any = ref();

const showDialog = () => {
    dialogVisible.value = true;
    imgSrc.value = avatarImg.value;
};
const setImage = (e: any) => {
    const file = e.target.files[0];
    if (!file.type.includes("image/")) {
        return;
    }
    const reader = new FileReader();
    reader.onload = (event: any) => {
        dialogVisible.value = true;
        imgSrc.value = event.target.result;
        cropper.value && cropper.value.replace(event.target.result);
    };
    reader.readAsDataURL(file);
    // console.log(file, "file");
    let formData = new FormData();
    formData.append("photo", file);
    console.log(formData);
    userUpdata(formData).then((data) => {
        console.log(data.picUrl);
        newImgUrl.value = data.picUrl;
    });
};

const cropImage = () => {
    cropImg.value = cropper.value.getCroppedCanvas().toDataURL();
};

const saveAvatar = () => {
    console.log(newImgUrl.value, user_msg.value.user_id);
    ElMessageBox.confirm("确定要更换吗？", "提示", { type: "warning" })
        .then(() => {
            save_img({ admin_avator: newImgUrl.value })
                .then((data) => {
                    if (data.code == 200) {
                        return oldImgUrl.value;
                    } else {
                        ElMessage.success(data.msg);
                    }
                })
                .then((x) => {
                    delete_img({ admin_avator: x }).then((data) => {
                        ElMessage.success(data.msg);
                        dialogVisible.value = false;
                        slu();
                    });
                });
        })
        .catch(() => {
            ElMessage.info("取消更换");
            dialogVisible.value = false;
        });
};
</script>

<style scoped>
.info {
    text-align: center;
    padding: 35px 0;
}

.info-image {
    position: relative;
    margin: auto;
    width: 100px;
    height: 100px;
    background: #f8f8f8;
    border: 1px solid #eee;
    border-radius: 50px;
    overflow: hidden;
}

.info-edit {
    display: flex;
    justify-content: center;
    align-items: center;
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    opacity: 0;
    transition: opacity 0.3s ease;
}

.info-edit i {
    color: #eee;
    font-size: 25px;
}

.info-image:hover .info-edit {
    opacity: 1;
}

.info-name {
    margin: 15px 0 10px;
    font-size: 24px;
    font-weight: 500;
    color: #262626;
}

.crop-demo-btn {
    position: relative;
}

.crop-input {
    position: absolute;
    width: 100px;
    height: 40px;
    left: 0;
    top: 0;
    opacity: 0;
    cursor: pointer;
}
</style>
